/**
 * @author yxw
 * date : 2014年5月19日 下午4:20:55 
 */
package com.gw.quotes.util;

import java.util.Calendar;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;

import com.yxw.util.OutLog;

public class DialogUtils {

	/**
	 * 带title的提示dialog
	 * 
	 * @param title
	 * @param content
	 * @param context
	 */
	public static void simpleDialog(String title, String content,
			Context context) {
		simpleDialog(title, content, context, null);
	}

	/**
	 * 带title的提示dialog
	 * 
	 * @param title
	 * @param content
	 * @param context
	 * @param callBack
	 */
	public static void simpleDialog(String title, String content,
			Context context, final DialogCallBack callBack) {
		try {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setTitle(title);
			builder.setMessage(content);
			builder.setCancelable(true);
			builder.setNegativeButton("确认", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (callBack != null) {
						callBack.onNegativeButton(null);
					}
				}
			});
			AlertDialog alertDialog = builder.create();
			// 点击框外退出dialog
			alertDialog.setCanceledOnTouchOutside(true);
			alertDialog.show();
		} catch (Exception e) {
			OutLog.LogE("gw", "simpleDialog:activity is not running!");
		}
	}

	/**
	 * title为“提示”的dialog
	 * 
	 * @param content
	 * @param context
	 */
	public static void alertDialog(String content, Context context) {
		alertDialog(content, context, null);
	}

	/**
	 * title为“提示”的dialog
	 * 
	 * @param content
	 * @param context
	 * @param callBack
	 */
	public static void alertDialog(String content, Context context,
			final DialogCallBack callBack) {
		alertDialog(content, context, true, callBack);

	}

	/**
	 * title为“提示”的dialog
	 * 
	 * @param content
	 * @param context
	 * @param canceledOnTouchOutside
	 * @param callBack
	 */
	public static void alertDialog(String content, Context context,
			boolean canceledOnTouchOutside, final DialogCallBack callBack) {
		try {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setTitle("提醒");
			builder.setMessage(content);
			builder.setCancelable(true);
			builder.setNegativeButton("确认", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (callBack != null) {
						callBack.onNegativeButton(null);
					}
				}
			});
			AlertDialog alertDialog = builder.create();
			// 点击框外退出dialog
			alertDialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
			alertDialog.show();
		} catch (Exception e) {
			OutLog.LogE("gw", "simpleDialog:activity is not running!");
		}
	}

	/**
	 * 两个按钮的dialog,用于验证
	 * 
	 * @param content
	 * @param context
	 * @param callBack
	 */
	public static void confirmDialog(String content, Context context,
			final DialogCallBack callBack) {
		try {
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setTitle("提醒");
			builder.setMessage(content);
			builder.setCancelable(true);
			builder.setPositiveButton("确定", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					if (callBack != null) {
						callBack.onPositiveButton(null);
					}
				}
			});
			builder.setNegativeButton("取消", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (callBack != null) {
						callBack.onNegativeButton(null);
					}
				}
			});
			AlertDialog alertDialog = builder.create();
			// 点击框外退出dialog
			alertDialog.setCanceledOnTouchOutside(true);
			alertDialog.show();
		} catch (Exception e) {
			OutLog.LogE("gw", "simpleDialog:activity is not running!");
		}
	}

	// public static void showDialogDateTime(Context context, String title,
	// final DialogCallBack<String> callBack) {
	// LayoutInflater inflater = (LayoutInflater) context
	// .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	// View mMenuView = inflater.inflate(R.layout.uc_dialog_date_time, null);
	// final TimePicker timePicker = (TimePicker) mMenuView
	// .findViewById(R.id.timePicker);
	// timePicker.setIs24HourView(true);
	// timePicker.setCurrentHour(new Date().getHours());
	// final DatePicker datePicker = (DatePicker) mMenuView
	// .findViewById(R.id.datePicker);
	// AlertDialog.Builder builder = new AlertDialog.Builder(context);
	// builder.setView(mMenuView);
	// builder.setTitle(title);
	// builder.setCancelable(true);
	// builder.setPositiveButton("确认", new OnClickListener() {
	//
	// @Override
	// public void onClick(DialogInterface dialog, int which) {
	// if (callBack != null) {
	//
	// String timeString = String.format(
	// "%d-%02d-%02d %02d:%02d:00", datePicker.getYear(),
	// datePicker.getMonth() + 1,
	// datePicker.getDayOfMonth(),
	// timePicker.getCurrentHour(),
	// timePicker.getCurrentMinute());
	// callBack.onPositiveButton(timeString);
	// }
	// }
	// });
	// builder.setNegativeButton("取消", new OnClickListener() {
	//
	// @Override
	// public void onClick(DialogInterface dialog, int which) {
	// if (callBack != null) {
	// callBack.onNegativeButton(null);
	// }
	// }
	// });
	// AlertDialog alertDialog = builder.create();
	// // 点击框外退出dialog
	// alertDialog.setCanceledOnTouchOutside(true);
	// alertDialog.show();
	// }

	/**
	 * 日期dialog
	 * 
	 * @param context
	 * @param v
	 */
	public static void showDialogDate(Context context,
			final DialogCallBack<String> callBack) {
		DatePickerDialog dateDialog = new DatePickerDialog(context,
				new OnDateSetListener() {
					@Override
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						if (callBack != null) {
							String date = String.format("%d-%02d-%02d", year,
									monthOfYear + 1, dayOfMonth);
							callBack.onPositiveButton(date);
						}
					}
				}, Calendar.getInstance().get(Calendar.YEAR), Calendar
						.getInstance().get(Calendar.MONTH), Calendar
						.getInstance().get(Calendar.DAY_OF_MONTH));
		dateDialog.setCancelable(true);
		dateDialog.setCanceledOnTouchOutside(true);
		dateDialog.show();
	}

	/**
	 * 时间dialog
	 * 
	 * @param context
	 * @param v
	 */
	// public static void showDialogTime(Context context, TextView v) {
	// final TextView textView = v;
	// TimePickerDialog timeDialog = new TimePickerDialog(context,
	// new OnTimeSetListener() {
	//
	// @Override
	// public void onTimeSet(TimePicker view, int hourOfDay,
	// int minute) {
	// textView.setText(String.format("%02d", hourOfDay) + ":"
	// + String.format("%02d", minute));
	// }
	// }, Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar
	// .getInstance().get(Calendar.MINUTE), true);
	// timeDialog.setCancelable(true);
	// timeDialog.setCanceledOnTouchOutside(true);
	// timeDialog.show();
	// }
	
	/** 带文本输入的dialog */
	public static void inputTitleDialog(String title, String content, Context context, final DialogCallBack callBack) {

		final EditText inputServer = new EditText(context);
		inputServer.setFocusable(true);

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(content);
		builder.setCancelable(true);
		// builder.setIcon(icon);
		builder.setView(inputServer).setNegativeButton("", null);
		
		builder.setPositiveButton("确定", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				String inputName = inputServer.getText().toString();
				if (callBack != null) {
					callBack.onPositiveButton(inputName);
				}
			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (callBack != null) {
					callBack.onNegativeButton(null);
				}
			}
		});
		
		builder.show();
	}
	
	public abstract static class DialogCallBack<T> {
		public DialogCallBack() {
			// TODO Auto-generated constructor stub
		}

		public void onPositiveButton(T t) {

		}

		public void onNegativeButton(Object o) {

		}

		public void onButton(Object o, int flag) {

		}
	}

}
